package com.salelogistics.basic.controller;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import javax.xml.crypto.Data;

import com.salelogistics.basic.utils.CheckParameterUtil;
import com.salelogistics.common.core.utils.StringUtils;
import com.salelogistics.common.security.utils.SecurityUtils;
import com.salelogistics.system.api.factory.RemoteFileFallbackFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.salelogistics.common.log.annotation.Log;
import com.salelogistics.common.log.enums.BusinessType;
import com.salelogistics.common.security.annotation.RequiresPermissions;
import com.salelogistics.basic.domain.BasSupplier;
import com.salelogistics.basic.service.IBasSupplierService;
import com.salelogistics.common.core.web.controller.BaseController;
import com.salelogistics.common.core.web.domain.AjaxResult;
import com.salelogistics.common.core.utils.poi.ExcelUtil;
import com.salelogistics.common.core.web.page.TableDataInfo;

/**
 * 供应商Controller
 *
 * @author ruoyi
 * @date 2023-07-07
 */
@RestController
@RequestMapping("supplier")
public class BasSupplierController extends BaseController {
    //日志打印类
    private static final Logger log = LoggerFactory.getLogger(BasSupplierController.class);

    //数据校验工具
    CheckParameterUtil checkParameterUtil = new CheckParameterUtil();

    @Autowired
    private IBasSupplierService basSupplierService;

    /**
     * 查询供应商列表
     */
    @RequiresPermissions("basic:supplier:list")
    @GetMapping("/list")
    public TableDataInfo list(BasSupplier basSupplier) {
        log.info("当前登录用户" + SecurityUtils.getUsername());
        startPage();
        List<BasSupplier> list = basSupplierService.selectBasSupplierList(basSupplier);
        return getDataTable(list);
    }

    /**
     * 导出供应商列表
     */
    @RequiresPermissions("basic:supplier:export")
    @Log(title = "供应商", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BasSupplier basSupplier) {

        List<BasSupplier> list = basSupplierService.selectBasSupplierList(basSupplier);
        ExcelUtil<BasSupplier> util = new ExcelUtil<BasSupplier>(BasSupplier.class);
        util.exportExcel(response, list, "供应商数据");
    }

    /**
     * 获取供应商详细信息
     */
    @RequiresPermissions("basic:supplier:query")
    @GetMapping(value = "/{supplierId}")
    public AjaxResult getInfo(@PathVariable("supplierId") Long supplierId) {
        return success(basSupplierService.selectBasSupplierBySupplierId(supplierId));
    }

    /**
     * 新增供应商
     */
    @RequiresPermissions("basic:supplier:add")
    @Log(title = "供应商", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BasSupplier basSupplier) {
        if (checkParameterUtil.checkParameter(basSupplier, "basSupplier")) {
            basSupplier.setCreateBy(SecurityUtils.getUsername());
            basSupplier.setEditedBy(SecurityUtils.getUsername());
            return toAjax(basSupplierService.insertBasSupplier(basSupplier));
        } else {
            return error("参数异常");
        }
    }

    /**
     * 修改供应商
     */
    @RequiresPermissions("basic:supplier:edit")
    @Log(title = "供应商", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BasSupplier basSupplier) {
        if (checkParameterUtil.checkParameter(basSupplier, "basSupplier")) {
            // 创建一个Date对象，表示当前时间
            Date date = new Date();
            // 创建一个SimpleDateFormat对象，指定时间格式
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            // 使用SimpleDateFormat对象格式化Date对象，获取当前时间的字符串表示
            String currentTime = sdf.format(date);
            basSupplier.setEditedBy(SecurityUtils.getUsername());
            basSupplier.setEditedTime(currentTime);
            return toAjax(basSupplierService.updateBasSupplier(basSupplier));
        } else {
            return error("参数异常");
        }
    }

    /**
     * 删除供应商
     */
    @RequiresPermissions("basic:supplier:remove")
    @Log(title = "供应商", businessType = BusinessType.DELETE)
    @DeleteMapping("/{supplierIds}")
    public AjaxResult remove(@PathVariable Long[] supplierIds) {
        return toAjax(basSupplierService.deleteBasSupplierBySupplierIds(supplierIds));
    }
}
